package xwbank.jmeter5.scripts.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import xwbank.jmeter5.utils.MQUtil;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class CmpRepayNoteTest extends AbstractJavaSamplerClient {
    private static MQUtil mqUtil = null;
    private static Channel channel=null;
    private static Connection connection = null;
    //private RabbitTemplate template = null;

    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
//        arguments.addArgument("host","");
//        arguments.addArgument("port","");
//        arguments.addArgument("username","");
//        arguments.addArgument("password","");
        arguments.addArgument("exchange","");
        arguments.addArgument("routeingKey","");
        arguments.addArgument("message","");
        return arguments;
    }

    @Override
    public void setupTest(JavaSamplerContext context) {
        mqUtil = new MQUtil();
        String iphost = "127.0.0.1";
        int portNum = 5672;
        String username = "guest";
        String password = "guest";
        try {
            connection = mqUtil.getConnection(iphost,portNum,username,password);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        try {
            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
//        String iphost = javaSamplerContext.getParameter("host");
//        String port = javaSamplerContext.getParameter("port");
//        int portNum = Integer.parseInt(port);
//        String username = javaSamplerContext.getParameter("username");
//        String password = javaSamplerContext.getParameter("password");
        String exchange = javaSamplerContext.getParameter("exchange");
        String routeingKey = javaSamplerContext.getParameter("routeingKey");
        String message = javaSamplerContext.getParameter("message");
        //CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        SampleResult result = new SampleResult();
        result.sampleStart();
        try {
//            connection = mqUtil.getConnection(iphost,portNum,username,password);
//            channel = connection.createChannel();
            AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                    .contentType("application/json")
                    .deliveryMode(2) // 传送方式
                    .priority(0)
                    .contentEncoding("UTF-8") // 编码方式
                    .build();
            channel.basicPublish(exchange, routeingKey, properties, message.getBytes("utf-8"));
            result.setResponseHeaders("response: " + "消息发送成功");
            result.setRequestHeaders("request: " + message);
            result.setDataType(SampleResult.TEXT);
            result.setSuccessful(true);
        } catch (IOException e) {
            result.setResponseHeaders("response: " + "消息发送失败");
            result.setRequestHeaders("request: " + message);
            result.setDataType(SampleResult.TEXT);
            result.setSuccessful(false);
            e.printStackTrace();
        } finally {
            result.sampleEnd();
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);
    }

    public static void main(String[] args) {
        Arguments arguments = new Arguments();
//        arguments.addArgument("host","127.0.0.1");
//        arguments.addArgument("port","5672");
//        arguments.addArgument("username","guest");
//        arguments.addArgument("password","guest");
        arguments.addArgument("exchange","loanInitQueue");
        arguments.addArgument("routeingKey","q.cmp-sch.loanInitQueue");
        arguments.addArgument("message","{\"ni hao\":\"good\"}");
        JavaSamplerContext con = new JavaSamplerContext(arguments);
        CmpRepayNoteTest queueModelTest = new CmpRepayNoteTest();
        queueModelTest.setupTest(con);
        queueModelTest.runTest(con);
        queueModelTest.teardownTest(con);
    }
}
